package ulmermark.demo.concurrency.core;

import java.util.Map;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppMain {
	private final String[] args;

	public AppMain(final String[] args) {
		this.args = args;
	}

	public static void main(String[] args){
		
		AppMain app = new AppMain( args);
		app.bootstrap();
	}

	private void bootstrap() {
		ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(this.args);
		
		Map<String, Resource> resources = appContext.getBeansOfType(Resource.class);
		
		init( resources);
		subscribeToTargets( resources);
		start( resources);
		
	}

	private void init(final Map<String, Resource> resources) {
		for(String beanName: resources.keySet()){
			Resource res = resources.get( beanName);
			res.onAppInit();
		}
	}
	
	private void start(Map<String, Resource> resources) {
		for(String beanName: resources.keySet()){
			Resource res = resources.get( beanName);
			res.start();
		}
		
	}

	private void subscribeToTargets(Map<String, Resource> resources) {
		for(String beanName: resources.keySet()){
			Resource res = resources.get( beanName);
			res.subscribeToTargets();
		}
		
	}

}
